package com.lam.system.service.impl;

import java.util.List;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.lam.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import com.lam.system.mapper.SysFrontMenuMapper;
import com.lam.system.entity.SysFrontMenu;
import com.lam.system.param.SysFrontMenuParam;
import com.lam.system.service.SysFrontMenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * 前端菜单Service业务层处理
 * 
 * @author cl
 * @date 2022-08-25
 */
@Service
public class SysFrontMenuServiceImpl implements SysFrontMenuService {


    @Autowired
    private SysFrontMenuMapper sysFrontMenuMapper;

    @Override
	public List<SysFrontMenu> selectByUserId(String uid) {

    	QueryWrapper<SysFrontMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.inSql("id", String.format("select rm.menu_id from sys_role_menu rm INNER JOIN sys_user_role ur on rm.role_id = ur.role_id where rm.type = 2 and ur.user_id='%s'", uid));
        return sysFrontMenuMapper.selectList(queryWrapper);
	}
    
    /**
     * 查询前端菜单
     * 
     * @param id ID
     * @return 单个对象
     */
    @Override
    @Transactional(readOnly = true)
    public SysFrontMenu selectById(String id) {

        return sysFrontMenuMapper.selectById(id);
    }

    /**
     * 查询前端菜单列表
     * 
     * @param param
     * @return 
     */
    @Override
    @Transactional(readOnly = true)
    public List<SysFrontMenu> selectList(SysFrontMenuParam param) {
    
        QueryWrapper<SysFrontMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(param.getParentId()), "parent_id", param.getParentId());
        queryWrapper.eq(StringUtils.isNotBlank(param.getMenuCode()), "menu_code", param.getMenuCode());
        queryWrapper.like(StringUtils.isNotBlank(param.getMenuNameKey()), "menu_name", param.getMenuNameKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getMenuName()), "menu_name", param.getMenuName());
        queryWrapper.eq(StringUtils.isNotBlank(param.getMenuColor()), "menu_color", param.getMenuColor());
        queryWrapper.eq(Objects.nonNull(param.getCreateTime()), "create_time", param.getCreateTime());
        queryWrapper.eq(StringUtils.isNotBlank(param.getRemark()), "remark", param.getRemark());

        return sysFrontMenuMapper.selectList(queryWrapper);
    }

    /**
     * 新增前端菜单
     * 
     * @param sysFrontMenu 
     * @return 
     */
    @Override
    @Transactional
    public int insert(SysFrontMenu sysFrontMenu) {
    
        return sysFrontMenuMapper.insert(sysFrontMenu);
    }

    /**
     * 修改前端菜单
     * 
     * @param sysFrontMenu 
     * @return 
     */
    @Override
    @Transactional
    public int update(SysFrontMenu sysFrontMenu) {
    
        return sysFrontMenuMapper.updateById(sysFrontMenu);
    }

    /**
     * 删除前端菜单信息
     * 
     * @param idStr ID，多个用逗号分隔
     * @return 
     */
    @Override
    @Transactional
    public int deleteById(String idStr) {
    	if(StringUtils.isBlank(idStr)) {
            return 0;
    	}
    	int n = 0;
    	for (String id : StringUtils.str2List(idStr)) {
            n += sysFrontMenuMapper.deleteById(id);
        }
        return n;
    }

}
